//
//  XMGiftInviteMessage.m
//  XunMi
//
//  Created by MacBook on 2024/4/15.
//  Copyright © 2024 TieZhu. All rights reserved.
//

#import "XMInviteMessage.h"

@implementation XMInviteMessage


+ (instancetype)giftMessageWithName:(NSString *)name
                              golds:(NSNumber *)golds
                              score:(NSString *)score
                            msgType:(NSNumber *)msgType
                             number:(NSNumber *)number
                            isVideo:(NSNumber *)isVideo
                              index:(NSNumber *)index
                            animUrl:(NSString *)animUrl
                             imgUrl:(NSString *)imgUrl {
    XMInviteMessage *msg = [[XMInviteMessage alloc] init];
    if (msg) {
        msg.name = name;
        msg.golds = golds;
        msg.score = score;
        msg.msgType = msgType;
        msg.number = number;
        msg.isVideo = isVideo;
        msg.index = index;
        msg.animUrl = animUrl;
        msg.imgUrl = imgUrl;
    }
    return msg;
}

+ (instancetype)guardMessageWithName:(NSString *)name
                              golds:(NSNumber *)golds
                              score:(NSString *)score
                             msgType:(NSNumber *)msgType {
    XMInviteMessage *msg = [[XMInviteMessage alloc] init];
    if (msg) {
        msg.name = name;
        msg.golds = golds;
        msg.score = score;
        msg.msgType = msgType;
    }
    return msg;
}

+ (instancetype)boxMessageWithName:(NSString *)name
                              golds:(NSNumber *)golds
                              score:(NSString *)score
                           msgType:(NSNumber *)msgType {
    XMInviteMessage *msg = [[XMInviteMessage alloc] init];
    if (msg) {
        msg.name = name;
        msg.golds = golds;
        msg.score = score;
        msg.msgType = msgType;
    }
    return msg;
}

+ (RCMessagePersistent)persistentFlag {
    return (MessagePersistent_ISPERSISTED | MessagePersistent_ISCOUNTED);
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    self = [super init];
    if (self) {
        self.name = [aDecoder decodeObjectForKey:@"name"];
        self.golds = [aDecoder decodeObjectForKey:@"golds"];
        self.score = [aDecoder decodeObjectForKey:@"score"];
        self.msgType = [aDecoder decodeObjectForKey:@"msgType"];
        self.number = [aDecoder decodeObjectForKey:@"number"];
        self.isVideo = [aDecoder decodeObjectForKey:@"isVideo"];
        self.index = [aDecoder decodeObjectForKey:@"index"];
        self.animUrl = [aDecoder decodeObjectForKey:@"animUrl"];
        self.imgUrl = [aDecoder decodeObjectForKey:@"imgUrl"];
    }
    return self;
}
- (void)encodeWithCoder:(NSCoder *)aCoder {
    [aCoder encodeObject:self.name forKey:@"name"];
    [aCoder encodeObject:self.golds forKey:@"golds"];
    [aCoder encodeObject:self.score forKey:@"score"];
    [aCoder encodeObject:self.msgType forKey:@"msgType"];
    [aCoder encodeObject:self.number forKey:@"number"];
    [aCoder encodeObject:self.isVideo forKey:@"isVideo"];
    [aCoder encodeObject:self.index forKey:@"index"];
    [aCoder encodeObject:self.animUrl forKey:@"animUrl"];
    [aCoder encodeObject:self.imgUrl forKey:@"imgUrl"];
}
- (NSData *)encode {
    NSMutableDictionary *dataDict = [NSMutableDictionary dictionary];
    if (self.name) {
        [dataDict setObject:self.name forKey:@"name"];
    }
    if (self.golds) {
        [dataDict setObject:self.golds forKey:@"golds"];
    }
    if (self.score) {
        [dataDict setObject:self.score forKey:@"score"];
    }
    if (self.msgType) {
        [dataDict setObject:self.msgType forKey:@"msgType"];
    }
    if (self.number) {
        [dataDict setObject:self.number forKey:@"number"];
    }
    if (self.isVideo) {
        [dataDict setObject:self.isVideo forKey:@"isVideo"];
    }
    if (self.index) {
        [dataDict setObject:self.index forKey:@"index"];
    }
    if (self.animUrl) {
        [dataDict setObject:self.animUrl forKey:@"animUrl"];
    }
    if (self.imgUrl) {
        [dataDict setObject:self.imgUrl forKey:@"imgUrl"];
    }
    if (self.senderUserInfo) {
        [dataDict setObject:[self encodeUserInfo:self.senderUserInfo] forKey:@"user"];
    }
    NSData *data = [NSJSONSerialization dataWithJSONObject:dataDict options:kNilOptions error:nil];
    return data;
}
- (void)decodeWithData:(NSData *)data {
    if (data) {
        __autoreleasing NSError *error = nil;
        NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
        if (dictionary) {
            self.name = [dictionary safe_objectForKey:@"name"];
            self.golds = [dictionary safe_objectForKey:@"golds"];
            self.score = [dictionary safe_objectForKey:@"score"];
            self.msgType = [dictionary safe_objectForKey:@"msgType"];
            self.number = [dictionary safe_objectForKey:@"number"];
            self.isVideo = [dictionary safe_objectForKey:@"isVideo"];
            self.index = [dictionary safe_objectForKey:@"index"];
            self.animUrl = [dictionary safe_objectForKey:@"animUrl"];
            self.imgUrl = [dictionary safe_objectForKey:@"imgUrl"];
            if (dictionary[@"user"]) {
                NSDictionary *userinfoDic = dictionary[@"user"];
                [self decodeUserInfo:userinfoDic];
            }
        }
    }
}

- (void)setValue:(id)value forUndefinedKey:(NSString *)key {
    NSLog(@"您设置的key：[%@]不存在", key);
    NSLog(@"您设置的value为：[%@]", value);
}

- (id)valueForUndefinedKey:(NSString *)key {
    NSLog(@"您访问的key:[%@]不存在", key);
    return nil;
}

- (NSString *)conversationDigest {
    if (self.msgType.intValue == 1) {
        return @"[礼物消息]";
    } else if (self.msgType.intValue == 2) {
        return @"[守护消息]";
    } else {
        return @"[盲盒消息]";
    }
}

+ (NSString *)getObjectName {
    return XMInviteMessageIdentifier;
}

@end
